<template>
  <div class="login">
    <el-row>
      <el-col :span="8" :offset="8">
        <!-- model:表单的数据 -->
        <!-- rules:表单的校验规则 -->

        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
         <h2 class='el-uTitle'>小u商城后台管理系统</h2>
          <el-input placeholder="请输入账号" v-model="ruleForm.username">
            <template slot="prepend">
              <i class='el-icon-user'></i>
            </template>
          </el-input>
            <el-input placeholder="请输入密码" v-model="ruleForm.password" show-password>
            <template slot="prepend">
              <i class='el-icon-lock'></i>
            </template>
          </el-input>
          <el-form-item>
            <el-button type="primary" @click="submitForm('')"
              >立即登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "admin888",
      },
      //检验规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 15, message: "长度在3到15个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 15, message: "长度在3到15个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm() {
      // this.$refs.ruleForm找到要校验的表单
      // validate element ui中自带的校验表单的函数
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          // 表单校验成功
          // alert("submit!");
          this.$http
            .post("/userlogin", this.ruleForm)
            .then(res => {
              if(res.msg==='登录成功'){
                // 将数据存在vuex中
                this.$store.commit('saveUserInfo',res.list);
                this.$router.push('/')
              }
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          // 表单校验失败
          // console.log('error submit!!');
          return false;
        }
      });
    },
  },
};
</script>

<style>
.login {
  width: 100vw;
  height: 100vh;
  background-image: url("../assets/loginbg.jpeg");
  background-size: cover;
}
.el-form {
  height: 40vh;
  background-color: #fff;
  margin-top: 45%;
  border-radius: 5px;
  box-shadow: #333 0 0 15px;
  padding:0 30px;
}

.el-input{
  padding:10px 0;
  margin-right:50px;
}
.el-button {
  margin-top:20px;
  background-color: #e9622d;
  padding:12px 100px;
}
.el-button--primary{
  border-color:#fff;
}
.el-uTitle{
  color:#e9622d;
  line-height:50px;
  text-align: center;
 
}


</style>